var systemForm = null;

$(document).ready (
	function() {
		systemForm = document.getElementById("systemForm");
		
		if (systemForm == null) {
			return;
		}
		
		/**
		 * Create hidden field for this form.
		 */
		systemForm.setValue = function(name, value) {
			try {
				eval("var ctrl = systemForm."+name);
			}
			catch(err) {
				var ctrl = undefined;
			}
			if (ctrl == undefined) {
				ctrl = document.createElement("input");
				ctrl.type = "hidden";
				ctrl.name = name;
				ctrl.id = name;
				systemForm.appendChild(ctrl);
			}
			ctrl.value = value;
		};
		
		/**
		 * Get the value for a key name
		 */
		systemForm.getValue = function(name) {
			eval("var val = systemForm."+name+".value");
			return val;
		};
		
		/**
		 *	Set the application value
		 */
		systemForm.setApp = function(value) {
			systemForm.setValue("app", value);
		};
		
		/**
		 *	Set the action value
		 */
		systemForm.setAct = function(value) {
			systemForm.setValue("act", value);
		};
		
		/**
		 *	Set the task value
		 */
		systemForm.setTask = function(value) {
			systemForm.setValue("task", value);
		};
		
		/**
		 * Validation function for validate inside data.
		 * CAUTION: this function should be implemented by the form that 
		 * 		want to check some think before submit. 
		 */
		systemForm.validate = function() {
			return true;
		};
		
		/**
		 * Submit
		 */
		systemForm.onsubmit = function() {
			return systemForm.validate();
		};
		
		/**
		 * Do ajax
		 * 
		 * callback: function(returnText) {}
		 */
		systemForm.doAjax = function(callback) {
			var data = $("#systemForm").serializeArray();
			var ajax = new Object();
			ajax.name = "ajax";
			ajax.value = "1";
			data[data.length] = ajax;
			$.post(SYS_ROOT_URL, data, callback);
		};
		
		/**
		 * Get Data for Ajax post
		 */
		systemForm.getAjaxData = function(data) {
			var formData = $("#systemForm").serializeArray();
			var ajax = new Object();
			ajax.name = "ajax";
			ajax.value = "1";
			formData[formData.length] = ajax;
			formData = formData.concat(data);
			return formData;
		};
		
		/**
		 * Load ajax into an elementId (normaly this should be a <div> or <span> tag)
		 * 
		 * callback: function(returnText) {}
		 */
		systemForm.loadAjax = function(elementId, data, callback) {
			var name = "#" + elementId;
			data = systemForm.getAjaxData(data);
			$(name).load(SYS_ROOT_URL, data, callback);
		};
	}
);

//////////////////////////////////////////////////////////////////////////////////////////////////////////

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function doAjax(formEle, callback) {
	var data = $(formEle).serializeArray();
	var ajax = new Object();
	ajax.name = "ajax";
	ajax.value = "1";
	data[data.length] = ajax;
	$.post(SYS_ROOT_URL, data, callback);
}
/////////////////////////////////////////////////////////////////////////////////////
// Check for the browser
////////////////////////////////////////////////////////////////////////////////////
var isIE = false;
var isGecko = false;
var isSafari = false;
function checkBrowser() {
	var sAgent = navigator.userAgent.toLowerCase() ;

 	// Internet Explorer
 	if ( sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 )
 	{
 	isIE = true;
	}
	// Gecko
	else if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 )
		isGecko = true;
	// Safari
	else if ( this.EnableSafari && sAgent.indexOf( 'safari' ) != -1 )
		isSafari = true;
	else
		return false ;
}
checkBrowser();

function emptyValue(ele) {
	if (ele.type.toLowerCase() != "file") {
		ele.value = "";
	}
	else {
		if (isIE){
			var size = ele.attributes.length
			var atb = "";
			for (i = 0; i < size; i++) {
				att = ele.attributes[i];
				if (att.nodeValue == null || att.nodeValue == "") {
					continue;
				}
				if (att.nodeName != "value") {
					atb = atb + att.nodeName + "=\"" + att.nodeValue + "\" ";
				}
				else {
					atb = atb + att.nodeName + "=\"\" ";
				}
			}
			ele.outerHTML = '<input '+ atb + '/>' ;
		}
		else {
			ele.value = "";
		}
	}
}